package com.axend.sofa.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import cn.com.heaton.blelibrary.ble.model.BleDevice
import com.axend.lib_base.R
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_base.engine.toast.toast
import com.axend.lib_base.help.ble.BleClient
import com.axend.lib_base.help.ble.BleMsgProcessor
import com.axend.lib_base.help.ble.enums.BleCommandTag
import com.axend.lib_base.utils.ByteUtil
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_base.utils.log.MyLogUtils
import com.axend.lib_base.view.dialog.CustomCalibrationDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CalibrationViewModel : BaseViewModel() {

    private var _isShowButton: MutableLiveData<Boolean> = MutableLiveData(true)
    val isShowButton: MutableLiveData<Boolean> get() = _isShowButton

    /**
     * dialogModel 0 loading 1 failed 2 success 3 dismiss
     */
    private var _dialogModel:MutableLiveData<Int> = MutableLiveData()
    val dialogModel: MutableLiveData<Int> get() = _dialogModel
    /**
     * 写入ble指令
     */
    fun writeBleMsg(msg: ByteArray, originData: Any = "") {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                BleClient.writeData(msg, object : BleClient.OnBleWriteCallback {
                    override fun onWriteSuccess(device: BleDevice, data: ByteArray) {
                        /*写入成功后返回的消息处理*/
                        val msgPair = BleMsgProcessor.decodeMsg(data)
                        msgPair?.let {
                            launchOnUI {
                                when (msgPair.first) {
                                    BleCommandTag.STUDY_RESULT -> {
                                        setDialogModel(CustomCalibrationDialog.MODEL_SUCCESS)
                                        val thresh1Byte = byteArrayOf(msgPair.second[0], msgPair.second[1],
                                            msgPair.second[2], msgPair.second[3])
                                        val thresh2Byte = byteArrayOf(msgPair.second[4], msgPair.second[5],
                                            msgPair.second[6], msgPair.second[7])
                                        MyLogUtils.w("thresh1Byte:${ByteUtil.byte4ToFloatLittle(thresh1Byte)}" +
                                                ",thresh2Byte:${ByteUtil.byte4ToFloatLittle(thresh2Byte)}")
                                    }

                                    BleCommandTag.SET_STUDY_START -> {
                                        when (msgPair.second[0]) {
                                            0.toByte() -> {
                                                setDialogModel(CustomCalibrationDialog.MODEL_FAILED)
                                            }

                                            1.toByte() -> {
                                                setDialogModel(CustomCalibrationDialog.MODEL_LOADING)
                                            }
                                        }
                                    }
                                    BleCommandTag.SET_STUDY_END -> {
                                        when (msgPair.second[0]) {
                                            0.toByte() -> {
                                                setDialogModel(CustomCalibrationDialog.MODEL_FAILED)
                                            }

                                            1.toByte() -> {
                                                setDialogModel(CustomCalibrationDialog.MODEL_DISMISS)
                                            }
                                        }
                                    }

                                    else -> {

                                    }
                                }
                            }
                        }

                    }

                    override fun onWriteFailed(device: BleDevice, errorCode: Int) {

                    }

                })
            }
        }
    }


    /**
     * 设置button是否显示
     */
    fun setButtonShow(isShow: Boolean) {
        launchOnUI {
            _isShowButton.postValue(isShow)
        }
    }

    /**
     * 设置dialogModel
     */
    fun setDialogModel(model: Int) {
        launchOnUI {
            _dialogModel.postValue(model)
        }
    }


}